package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.utils.Constants;
import com.qiantu.phone.event.RefreshFloorRoomEvent;
import com.qiantu.phone.ui.adapter.SelectFloorAdapter;
import com.qiantu.phone.ui.dialog.BottomEditDialog;
import com.lxj.xpopup.XPopup;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class SelectFloorActivity extends AppActivity {
    private RecyclerView list;
    private SelectFloorAdapter adapter;
    private BottomEditDialog mBottomEditDialog;
    private String roomSerialNo;
    private String roomName;
    private String floorSerialNo;
    private String roomType;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_floor;
    }

    @Override
    protected void initView() {
        roomSerialNo = getIntent().getStringExtra("roomSerialNo");
        roomName = getIntent().getStringExtra("roomName");
        floorSerialNo = getIntent().getStringExtra("floorSerialNo");
        roomType = getIntent().getStringExtra("roomType");
        list = findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SelectFloorAdapter(getContext()){
            @Override
            public void onSelectChanged(FloorBean selected) {
                LLHttpManager.editRoom((LifecycleOwner) getContext(), roomSerialNo, selected.getFloorSerialNo(), roomName, roomType, new HttpCallback<HttpData<Void>>(null){
                    @Override
                    public void onSucceed(HttpData<Void> result) {
                        LLRoomDBManager.getInstance(getContext()).editRoomInfo(roomSerialNo, roomName, selected.getFloorSerialNo(),roomType);
                        Intent data = new Intent();
                        data.putExtra("floorSerialNo", selected.getFloorSerialNo());
                        data.putExtra("floorName", selected.getFloorName());
                        EventBus.getDefault().post(new RefreshFloorRoomEvent());
                        setResult(RESULT_OK, data);
                        finish();
                        toast(R.string.reset_success);
                    }
                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        toast("修改失败");
                    }
                });
            }
        };
        list.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        List<FloorBean> floorBeans = LLFloorDBManager.getInstance(getContext()).getAll();
        adapter.setData(floorBeans);
        if(floorBeans != null){
            for(FloorBean floor : floorBeans){
                if(floor.getFloorSerialNo().equals(floorSerialNo)){
                    adapter.setSelected(floor);
                    break;
                }
            }
        }
    }

    @Override
    public void onRightClick(View view) {
        showBottomEditDialog();
    }

    private void showBottomEditDialog() {
        if (mBottomEditDialog == null) {
            mBottomEditDialog = new BottomEditDialog(this);
            mBottomEditDialog.setDialogClickListener(new BottomEditDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {
                    creatFloor(mBottomEditDialog.getText());
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomEditDialog);
        }
        mBottomEditDialog.setLabel(getString(R.string.floor_name));
        mBottomEditDialog.setMaxLength(Constants.FLOOR_NAME_MAX_LENGTH);
        mBottomEditDialog.show();
    }

    private void creatFloor(String floorName) {
        LLHttpManager.addFloor(this, AppApplication.getInstance().getCurrentHouseSerialNo(), floorName, new HttpCallback<HttpData<FloorBean>>(this){
            @Override
            public void onSucceed(HttpData<FloorBean> result) {
                super.onSucceed(result);
                setResult(RESULT_OK);
                LLFloorDBManager.getInstance(getContext()).put(result.getData());
                toast("添加成功");
                initData();
            }
        });
    }
}